package com.faasadmin.faas.modules.admin.app.config;

import cn.hutool.core.util.ObjectUtil;
import com.faasadmin.faas.addonss.admin.app.config.vo.AppConfigReqVO;
import com.faasadmin.faas.business.core.support.controller.BaseController;
import com.faasadmin.faas.services.tenant.dal.dataobject.config.TenantConfigDO;
import com.faasadmin.faas.services.tenant.service.config.TenantConfigService;
import com.faasadmin.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.faasadmin.framework.common.pojo.CommonResult.success;


/**
 * @author: faasadmin
 * @description: 配置
 * @data: 2022-07-16 14:05
 **/
@Tag(name = "配置")
@RestController
@RequestMapping("/app/config")
@Validated
public class AppConfigController extends BaseController {

    @Autowired
    private TenantConfigService tenantConfigService;

    /**
     * 获取配置信息
     *
     * @return
     */
    @PostMapping("get")
    public CommonResult<Map<String, String>> get(@RequestBody AppConfigReqVO reqVO) {
        Long tenantId = getHeaderTenantId();
        String headerAppShort = getHeaderAppShort();
        Map<String, String> map = new HashMap<String, String>();
        String keys = reqVO.getKey();
        String[] split = keys.split(",");
        for (String key : split) {
            TenantConfigDO config = tenantConfigService.getTenantConfigByKey(tenantId, headerAppShort, key);
            if (ObjectUtil.isNotEmpty(config) && config.getVisible()) {
                map.put(key, config.getConfigValue());
            }
        }
        return success(map);
    }

}
